<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if="goods">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
            goods.categories[1].name
          }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[1].id}`">{{
            goods.categories[0].name
          }}
        </XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"/>
          <GoodsSales/>
        </div>
        <div class="spec">
          <!-- 商品名字区组件 -->
          <GoodsName :goods="goods"/>
          <!--  商品规格组件-->
          <GoodsSku :goods="goods" @change="changeSku"/>
          <!--  商品数量选择组件-->
          <XtxNumbox v-model="num" :max="goods.inventory" :label="'数量'"/>
          <!--   按钮组件-->
          <XtxButton @click="insertCart()" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs/>
          <!-- 注意事项 -->
          <GoodsWarn/>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"/>
          <GoodsHot :type="2"/>
          <GoodsHot :type="3"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import XtxBread from '@/components/library/xtx-bread'
import XtxBreadItem from '@/components/library/xtx-bread-item'
import { nextTick, provide, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/product'
import GoodsImage from '@/views/goods/components/goods-image'
import GoodsSales from '@/views/goods/components/goods-sales'
import GoodsName from '@/views/goods/components/goods-name'
import GoodsSku from '@/views/goods/components/goods-sku'
import XtxNumbox from '@/components/library/xtx-numbox'
import XtxButton from '@/components/library/xtx-button'
import GoodsTabs from '@/views/goods/components/goods-tabs'
import GoodsHot from '@/views/goods/components/goods-hot'
import GoodsWarn from '@/views/goods/components/goods-warn'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsWarn,
    GoodsHot,
    GoodsTabs,
    XtxButton,
    XtxNumbox,
    GoodsSku,
    GoodsName,
    GoodsSales,
    GoodsImage,
    XtxBreadItem,
    XtxBread,
    GoodsRelevant
  },
  setup () {
    const currSku = ref(null)
    //  1.获取商品详情,进行渲染
    const goods = useGoods()
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      currSku.value = sku
    }
    // 提供goods数据给后代组件使用
    provide('goods', goods)

    const store = useStore()
    // 加入购物车
    const insertCart = () => {
      if (currSku.value && currSku.value.skuId) {
        const {
          skuId,
          specText: attrsText,
          inventory: stock
        } = currSku.value
        const {
          id,
          name,
          price,
          mainPictures
        } = goods.value
        // id skuId name attrsText picture price nowPrice selected stock count isEffective
        store.dispatch('cart/insertCart', {
          id,
          skuId,
          name,
          attrsText,
          price,
          stock,
          nowPrice: price,
          picture: mainPictures[0],
          selected: true,
          isEffective: true,
          count: num.value
        }).then(() => {
          Message({
            type: 'success',
            text: '加入购物车成功'
          })
        })
      } else {
        Message({ text: '请选择完整规则' })
      }
    }

    // 选择的数量
    const num = ref(1)
    return {
      goods,
      num,
      changeSku,
      insertCart
    }
  }
}

// 获取商品详情信息
const useGoods = () => {
  const goods = ref(null)
  const route = useRoute()
  // 出现路由地址的商品ID发生变化,但是不会重新渲染组件
  watch(() => route.params.id, () => {
    if (route.path === '/product/' + route.params.id) {
      findGoods(route.params.id).then(data => {
        // 让商品数据为空.然后使用v-if的组件可以重新销毁和创建,
        goods.value = null
        // 当改完一个数,发现无法操作这个数据的DOM,因为数据还没有生效,就跨域使用nextTick当数据生效是再操作
        nextTick(() => {
          goods.value = data.result
        })
      })
    }
  }, { immediate: true })
  return goods
}

</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

//.goods-tabs {
//  min-height: 600px;
//  background: #fff;
//}

//.goods-warn {
//  min-height: 600px;
//  background: #fff;
//  margin-top: 20px;
//}
</style>
